package cn.ac.iscas.crawlers;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.security.SecureRandom;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;


public class ZXW {

	static String urlsJson = "{\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MiwxLDAsMA%3D%3D.html\": \"处理器及微控制器\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTA0LDEsMCww.html\": \"微控制器\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTA1LDEsMCww.html\": \"ATMEL(爱特美尔)/AVR\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTA2LDEsMCww.html\": \"ST(意法半导体)\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTA3LDEsMCww.html\": \"MICROCHIP(美国微芯)\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTA4LDEsMCww.html\": \"ADI(亚德诺)\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTA5LDEsMCww.html\": \"NXP(恩智浦)\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTEwLDEsMCww.html\": \"TI(德州仪器)\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTExLDEsMCww.html\": \"STC(宏晶)\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTEyLDEsMCww.html\": \"FREESCALE(飞思卡尔）\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTEzLDEsMCww.html\": \"NUVOTON(新唐)\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTE0LDEsMCww.html\": \"CYPRESS(赛普拉斯)\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTE1LDEsMCww.html\": \"SST(超捷)\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTE2LDEsMCww.html\": \"SYNCMOS(新茂)\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTE3LDEsMCww.html\": \"MEGAWIN(笙泉)\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTE4LDEsMCww.html\": \"RENESAS(瑞萨)\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MzM5LDEsMCww.html\": \"处理器及微控制器\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MywxLDAsMA%3D%3D.html\": \"嵌入式外围芯片\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTIwLDEsMCww.html\": \"字库\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTIxLDEsMCww.html\": \"端口扩展\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTIyLDEsMCww.html\": \"MCU监控芯片\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTIzLDEsMCww.html\": \"驱动芯片\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTI0LDEsMCww.html\": \"实时时钟\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/NCwxLDAsMA%3D%3D.html\": \"逻辑芯片\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTI1LDEsMCww.html\": \"74系列逻辑芯片\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTI2LDEsMCww.html\": \"4000系列逻辑芯片\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTI3LDEsMCww.html\": \"时基集成\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTI4LDEsMCww.html\": \"CPLD/FPGA\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTI5LDEsMCww.html\": \"逻辑芯片\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/NSwxLDAsMA%3D%3D.html\": \"存储器\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTMwLDEsMCww.html\": \"PROM\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTMxLDEsMCww.html\": \"FLASH\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTMyLDEsMCww.html\": \"EEPROM\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTMzLDEsMCww.html\": \"RAM\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTM0LDEsMCww.html\": \"FRAM\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/NiwxLDAsMA%3D%3D.html\": \"电源芯片\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTM2LDEsMCww.html\": \"电源模块\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTM3LDEsMCww.html\": \"线性稳压\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTM4LDEsMCww.html\": \"低压差线性稳压(LDO)\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTM5LDEsMCww.html\": \"DC-DC芯片\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTQwLDEsMCww.html\": \"开关电源芯片\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTQxLDEsMCww.html\": \"电压基准\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTQyLDEsMCww.html\": \"电源芯片\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTQzLDEsMCww.html\": \"电池电源管理\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/NywxLDAsMA%3D%3D.html\": \"放大器、线性器件\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTQ0LDEsMCww.html\": \"通用运放\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTQ1LDEsMCww.html\": \"放大器\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTQ2LDEsMCww.html\": \"高速、宽带运放\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTQ3LDEsMCww.html\": \"仪表运放\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTQ4LDEsMCww.html\": \"精密运放\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTQ5LDEsMCww.html\": \"FET输入运放\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTUwLDEsMCww.html\": \"低噪声运放\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTUxLDEsMCww.html\": \"电压比较器\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTUyLDEsMCww.html\": \"低功耗运放\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTUzLDEsMCww.html\": \"音频放大器\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTU0LDEsMCww.html\": \"差分运放\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/OCwxLDAsMA%3D%3D.html\": \"模拟芯片\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTU1LDEsMCww.html\": \"模数转换\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTU2LDEsMCww.html\": \"数模转换\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTU3LDEsMCww.html\": \"模拟芯片\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTU4LDEsMCww.html\": \"模拟开关\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTU5LDEsMCww.html\": \"电量计\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTYwLDEsMCww.html\": \"数字电位器\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/OSwxLDAsMA%3D%3D.html\": \"接口芯片\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTYxLDEsMCww.html\": \"USB\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTYyLDEsMCww.html\": \"RS232\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTYzLDEsMCww.html\": \"RS-485/RS-422\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTY0LDEsMCww.html\": \"以太网\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTY1LDEsMCww.html\": \"隔离器\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTY3LDEsMCww.html\": \"音频接口芯片\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTY4LDEsMCww.html\": \"DDS\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTY5LDEsMCww.html\": \"CAN\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTcwLDEsMCww.html\": \"LVDS\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTcxLDEsMCww.html\": \"接口芯片\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTcyLDEsMCww.html\": \"无线收发芯片\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTAsMSwwLDA%3D.html\": \"驱动器\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTczLDEsMCww.html\": \"IGBT驱动\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTc0LDEsMCww.html\": \"LED驱动\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTc1LDEsMCww.html\": \"LCD驱动\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTc2LDEsMCww.html\": \"MOS驱动\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTc3LDEsMCww.html\": \"电机驱动\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTc4LDEsMCww.html\": \"可控硅驱动\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTc5LDEsMCww.html\": \"大电流驱动\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTgwLDEsMCww.html\": \"驱动芯片\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTEsMSwwLDA%3D.html\": \"传感器\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTgxLDEsMCww.html\": \"传感器\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTgyLDEsMCww.html\": \"温度传感器\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTgzLDEsMCww.html\": \"颜色传感器\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTg0LDEsMCww.html\": \"图像传感器\",\r\n"
			+ "  \"https://www.8gau.com.safe.cdntip.com/category/MTg1LDEsMCww.html\": \"加速度传感器\"\r\n"
			+ "}";
	
	private static final ObjectMapper mapper = new ObjectMapper()
            .enable(SerializationFeature.INDENT_OUTPUT);
	
	public static void main(String[] args) throws Exception {
		// 创建 ObjectMapper 实例
        ObjectMapper objectMapper = new ObjectMapper();

        // 解析 JSON
        JsonNode rootNode = objectMapper.readTree(urlsJson);

        // 遍历所有字段
        Iterator<Map.Entry<String, JsonNode>> fields = rootNode.fields();
        while (fields.hasNext()) {
            Map.Entry<String, JsonNode> entry = fields.next();
            String url = entry.getKey();
            String productName = entry.getValue().asText();
            System.out.println("URL: " + url + ", 产品名称: " + productName);
            
            try {
                List<Map<String, String>> result = parseComponentData(url);
                String json = mapper.writeValueAsString(result);
//                System.out.println(json);
                
                FileWriter fw = new FileWriter(new File(productName + ".json"));
                fw.write(json);
                fw.close();
            } catch (IOException e) {
                System.err.println("Error fetching or parsing data: " + e.getMessage());
            }
        }
		
		
	}

	public static List<Map<String, String>> parseComponentData(String url) throws Exception {
        // 设置请求头
        Map<String, String> headers = new HashMap<>();
        headers.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.124 Safari/537.36");
        headers.put("Referer", "https://www.8gau.com.safe.cdntip.com/");
        headers.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
        headers.put("Accept-Language", "zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3");
//        headers.put(":authority:", "www.8gau.com.safe.cdntip.com");

        disableSSLCertValidation();
        // 获取HTML文档
        Document doc = Jsoup.connect(url)
                .headers(headers)
                .timeout(10000)
                .get();

        // 解析数据
        List<Map<String, String>> components = new ArrayList<>();
        Elements rows = doc.select("table.pricelist tr:not(.th)");

        for (Element row : rows) {
            Elements cells = row.select("td");
            if (cells.size() >= 5) {
                Map<String, String> component = new HashMap<>();
                component.put("model", cells.get(0).text());
                component.put("producer", cells.get(1).text());
                component.put("gongyi", cells.get(2).text());
                component.put("kucun", cells.get(3).text());
                component.put("price", "0"); // 价格默认为0
                components.add(component);
            }
        }

        return components;
    }
	
	public static void disableSSLCertValidation() throws Exception {
	    TrustManager[] trustAllCerts = new TrustManager[]{
	        new X509TrustManager() {
	            public X509Certificate[] getAcceptedIssuers() { return null; }
	            public void checkClientTrusted(X509Certificate[] certs, String authType) {}
	            public void checkServerTrusted(X509Certificate[] certs, String authType) {}
	        }
	    };

	    SSLContext sc = SSLContext.getInstance("TLS");
	    sc.init(null, trustAllCerts, new SecureRandom());
	    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

	    HostnameVerifier allHostsValid = (hostname, session) -> true;
	    HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
	}
}